﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HCommon;
using System.Threading;
using eStock.hControl;
namespace eStock
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
            CheckForIllegalCrossThreadCalls = false;
            txtUserName.Focus();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                Thread tienTrinh = new Thread(new ThreadStart(StartLogin));
                tienTrinh.SetApartmentState(ApartmentState.STA);
                tienTrinh.Start();
            }
            catch (Exception ex)
            {
                Common.GhiLog("btnLogin_Click", ex);
                Dialog.ShowMess(Dialog.mType.err, ex.Message);
            }
        }

        private void txtUserPwd_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnLogin.PerformClick();
            }
        }

        private void StartLogin()
        {
            DataTable dt = new DataTable();
            dt = Dao.Get_Table("SELECT TOP 1 * FROM tblUSER WITH(NOLOCK) WHERE USER_NAME = '" + Common.xoaNhay(txtUserName.Text) + "' AND USER_PWD = '" + Common.xoaNhay(txtUserPwd.Text) + "'");
            if (dt.Rows.Count == 0) // HoaiVQ - Chưa mã hóa
            {
                Dialog.ShowMess(Dialog.mType.warn, "Tài khoản hoặc mật khẩu không chính xác!");
                return;
            }

            // Load các thông số
            Settings.myUserName = txtUserName.Text;
            Settings.myGroup = dt.Rows[0]["Group_Cd"].ToString();
            Settings.myUserCode = dt.Rows[0]["user_code"].ToString();
            // End Load các thông số
            this.Close();
            Application.Run(new frmMDIMain());
        }
    }
}
